package yeliuhuishi.File;

import java.io.File;

public class DelAllFile {
    /**
     * 删除文件或文件夹
     */
    public static void delAllFile(File directory) {
        if (!directory.isDirectory()) {
            boolean delete = directory.delete();
            System.out.print(delete ? "成功删除:" : "删除失败:");
        } else {
            File[] files = directory.listFiles();
            // 空文件夹

            assert files != null;
            if (files.length == 0) {
                boolean delete = directory.delete();
                System.out.print(delete ? "成功删除:" : "删除失败:");
            } else {// 删除子文件夹和子文件
                for (File file : files) {
                    if (file.isDirectory()) {
                        delAllFile(file);
                    } else {

                        boolean delete = file.delete();
                        System.out.print(delete ? "成功删除:" : "删除失败:");
                        System.out.println(file.getPath());
                    }
                }// 删除文件夹本身
                boolean delete = directory.delete();
                System.out.print(delete ? "成功删除:" : "删除失败:");
            }
            System.out.println(directory.getPath());


        }
    }
}